package com.can.filter;



import com.can.Utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Component
public class auth extends AbstractGatewayFilterFactory<Object> {
    @Autowired
    JwtUtil jwtUtil;
    @Override
    public GatewayFilter apply(Object config) {
        return new GatewayFilter() {

            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                // 获取请求
                String path = exchange.getRequest().getURI().getPath();

                String token = exchange.getRequest().getHeaders().getFirst("token");
//                ,"/post/createReply"
//                List<String> pathsToAuthenticate = Arrays.asList("/post/createReply","/post/createPost"
//                        ,"/user/wode","post/deletepost");
//
////                不在要求路径直接放行
//                if (!pathsToAuthenticate.contains(path)) {
//                    System.out.println("请求路径:"+path+"没走过滤器");
//                    return chain.filter(exchange);
//                }
//                System.out.println("请求路径:"+path+"走了过滤器");
                System.out.println("请求路径:"+path+"走了过滤器");
                try {
                    // 解析并校验 JWT
                    Claims claims = jwtUtil.parseToken(token);

                    // 如果过期直接返回401
                    if (claims.getExpiration().before(new Date())) {
                        System.out.println("过期了");
                        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                        return exchange.getResponse().setComplete();
                    }
                    //没过期就解析信息
                    String userId = claims.getSubject();
                    Integer power = claims.get("power", Integer.class);
                    String username = claims.get("username", String.class);
                    System.out.println("username:"+username);
                    //校验通过
                    ServerWebExchange modifiedExchange = exchange.mutate()
                            .request(builder -> builder.header("X-UserId", userId)
                                    .header("X-Power", String.valueOf(power)
                                    ).header("X-Username", username))
                            .build();
                    return chain.filter(exchange);

                    // 如果 JWT 无效，返回 401
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                    System.out.println("无效");
                    exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                    return exchange.getResponse().setComplete();}




            }
        };
    }
}